package org.darkforest.common.services.impl;

import org.darkforest.common.services.GoodsService;
import org.darkforest.common.services.MessageAndNoticeService;
import org.darkforest.dao.mapper.GoodsCommonMapper;
import org.darkforest.dao.mapper.GoodsMapper;
import org.darkforest.model.GoodSearchBean;
import org.darkforest.model.Goods;
import org.darkforest.model.GoodsCommon;
import org.darkforest.model.Order;
import org.darkforest.vo.PriceRange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created by Libing on 2015/7/11.
 */
@Service
@Transactional
public class GoodsServiceImpl implements GoodsService {


    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private GoodsCommonMapper goodsCommonMapper;
    @Autowired
    private MessageAndNoticeService messageAndNoticeService;

    /**
     * 批量修改商品价格
     *
     * @param goodsIdMap
     */
    @Override
    public void updateGoodsPriceBath(Map<String, Object> goodsIdMap) {
        goodsMapper.updateGoodsPriceBath(goodsIdMap);
    }

    @Override
    public List<Goods> selectByCondition(Goods goods) {
        return goodsMapper.findGoodsByCondition(goods);
    }

    @Override
    public void updateByPrimaryKeySelective(Goods goods) {
        goodsMapper.updateByPrimaryKeySelective(goods);
    }

    @Override
    public void updateByPrimaryKeySelective(GoodsCommon goodsCommon) {
        goodsCommonMapper.updateByPrimaryKeySelective(goodsCommon);
    }

    /**
     * 根据主键查询商品信息
     *
     * @param goods_id
     * @return
     */
    @Override
    public Goods selectGoodsByPrimaryKey(Integer goods_id) {
        return goodsMapper.selectById(goods_id);
    }

    @Override
    /**
     * 批量修改商品状态
     */
    public void modifyGoodsStatusByBatch(String[] goodsCommonIds, String goodsStatus) {

//        String[] commonids = goodsCommonIds;
        List<String> goodsCommondIdsList = new ArrayList<String>();
        Collections.addAll(goodsCommondIdsList, goodsCommonIds);
        Map conditionMap = new HashMap();
        conditionMap.put("goodsStatus", goodsStatus);
        conditionMap.put("goodsCommonIdsList", goodsCommondIdsList);
        goodsCommonMapper.modifyGoodsStatusByBatch(conditionMap);
        if(goodsStatus=="0"){
            for(String id:goodsCommondIdsList){
                GoodsCommon goodsCommon =goodsCommonMapper.selectByPrimaryKey(Integer.parseInt(id));
                messageAndNoticeService.sendGoodsViolationToStore(goodsCommon);
            }
        }
    }

    /**
     * 批量修改商品状态
     */
    public void goodsVerifyByBatch(Integer[] goodsCommonIds, Integer goodsVerify,Integer goodsStatus) {
        List<Integer> goodsCommondIdsList = new ArrayList<Integer>();
        Collections.addAll(goodsCommondIdsList, goodsCommonIds);
        Map conditionMap = new HashMap();
        conditionMap.put("goodsVerify", goodsVerify);
        conditionMap.put("goodsStatus", goodsStatus);
        conditionMap.put("goodsCommonIdsList", goodsCommondIdsList);
        goodsCommonMapper.goodsVerifyByBatch(conditionMap);

    }

    /**
     * 还原库存
     *
     * @param goods
     */
    @Override
    public void restoreGoodsStorege(Goods goods) {
        goodsMapper.restoreGoodsStorege(goods);
    }

    /**
     * 根据店铺id以及分类id下架商品
     *
     * @param map
     */
    @Override
    public void soldOutStoreGoods(Map map) {
        goodsMapper.soldOutStoreGoods(map);
    }

    public Map<String, Object> findPriceSaleByCondition(GoodSearchBean goods, List<PriceRange> priceRanges) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("startTime",goods.getStartTime());
        paramMap.put("endTime",goods.getEndTime());
        paramMap.put("goodsClassId",goods.getGoodsClassId());
        paramMap.put("priceRangeList",priceRanges);
        return goodsMapper.findPriceSaleByCondition(paramMap);
    }

    @Override
    public List<Map<String, Object>> findTimeRangeByCondition(GoodSearchBean goods) {
        return goodsMapper.findTimeRangeByCondition(goods);
    }
}
